package cn.findviewbydream.vant.plugin.docanalysis;

import com.google.gson.Gson;
import cucumber.api.java.ca.I;
import cucumber.api.java.it.Data;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * cn.findviewbydream.vant.plugin.docanalysis
 *
 * @author 白雨浓
 * date: 2019/11/11 18:31
 * content:
 */
public class DocAnalysis {

    private static final String ROOT = "F:/other/vant/src";

    public static void main(String[] args) throws IOException {

        List<String> nameList = Files.list(Paths.get(ROOT))
                .filter(path -> path.toFile().isDirectory())
                .map(Path::getFileName)
                .map(Path::toString)
                .collect(Collectors.toList());

        /*Map<String, Item> map = new HashMap<>();
        for (String name : nameList) {

            Path path = Paths.get(ROOT + "/" + name + "/README.zh-CN.md");

            if (Files.notExists(path)) {
                continue;
            }

            String content = new String(Files.readAllBytes(path));
            Item item = handle(name, content);
            map.put("van-" + name, item);
        }

        System.out.println(new Gson().toJson(map));*/

        Item item = handle("1", "### RadioGroup Props\n" +
                "\n" +
                "| 参数 | 说明 | 类型 | 默认值 | 版本 |\n" +
                "|------|------|------|------|------|\n" +
                "| v-model | 当前选中项的标识符 | *any* | - | - |\n" +
                "| disabled | 是否禁用所有单选框 | *boolean* | `false` | - |\n" +
                "| icon-size | 所有单选框的图标大小，默认单位为`px` | *string \\| number* | `20px` | 2.2.3 |\n" +
                "| checked-color | 所有单选框的选中状态颜色 | *string* | `#1989fa` | 2.2.3 |\n"+
                "### RadioGroup Events\n" +
                "\n" +
                "| 事件名 | 说明 | 回调参数 |\n" +
                "|------|------|------|\n" +
                "| change | 当绑定值变化时触发的事件 | 当前选中项的 name |");
        System.out.println(new Gson().toJson(item));

    }

    private static Item handle(String name, String content) {

        String[] lines = content.split("\n");

        Item item = new Item();

        /*props*/
        {
            List<String> list = new ArrayList<>();
            boolean mark = false;
            for (String line : lines) {
                if (!mark && line.startsWith("| 参数 |")) {
                    mark = true;
                }
                if (mark && (line.startsWith("###") || line.trim().length() == 0)) {
                    break;
                }
                if (mark) {
                    list.add(line);
                }
            }

            if (list.size() > 2) {
                list = list.subList(2, list.size());
            }

            List<Prop> props = new ArrayList<>();
            for (String s : list) {

                if (s.trim().length() == 0) {
                    continue;
                }

                s = s.substring(1, s.length() - 1);
                String[] ss = s.split(" [|] ");
                Prop prop = new Prop();
                prop.setProp(ss[0].trim());
                prop.setDesc(ss[1].trim());

                if(!ss[2].trim().equals("-")){
                    String type = ss[2].trim();
                    type = type.substring(1, type.length() - 1);

                    prop.setTypes(Arrays.stream(type.split("\\\\[|]"))
                            .map(String::trim)
                            .collect(Collectors.toList()));
                }

                if (!ss[3].trim().equals("-")) {
                    prop.setDefaultValue(ss[3].trim().replace("`", ""));
                }
                try {
                    if (!ss[4].trim().equals("-")) {
                        prop.setVersion(ss[4].trim());
                    }
                } catch (Exception ignored) {
                }

                props.add(prop);
            }

            item.setProps(props);
        }

        /*props*/
        {
            List<String> list = new ArrayList<>();
            boolean mark = false;
            for (String line : lines) {
                if (!mark && line.startsWith("| 事件名 |")) {
                    mark = true;
                }
                if (mark && (line.startsWith("###") || line.trim().length() == 0)) {
                    break;
                }
                if (mark) {
                    list.add(line);
                }
            }

            if (list.size() > 2) {
                list = list.subList(2, list.size());
            }

            List<Method> methods = new ArrayList<>();
            for (String s : list) {

                if (s.trim().length() == 0) {
                    continue;
                }

                s = s.substring(1, s.length() - 1);
                String[] ss = s.split(" [|] ");

                Method method = new Method();
                method.setMethod(ss[0].trim());
                method.setDesc(ss[1].trim());
                try {
                    if (!ss[2].trim().equals("-")) {
                        method.setParam(ss[2].trim());
                    }
                } catch (Exception ignored) {
                }

                methods.add(method);
            }

            item.setMethods(methods);
        }

        return item;
    }

    private static class Item {

        private List<Prop> props;

        private List<Method> methods;

        public List<Prop> getProps() {
            return props;
        }

        public void setProps(List<Prop> props) {
            this.props = props;
        }

        public List<Method> getMethods() {
            return methods;
        }

        public void setMethods(List<Method> methods) {
            this.methods = methods;
        }
    }

    private static class Prop {

        private String prop;

        private String desc;

        private List<String> types;

        private String defaultValue;

        private String version;

        public String getProp() {
            return prop;
        }

        public void setProp(String prop) {
            this.prop = prop;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public List<String> getTypes() {
            return types;
        }

        public void setTypes(List<String> types) {
            this.types = types;
        }

        public String getDefaultValue() {
            return defaultValue;
        }

        public void setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }
    }


    private static class Method {

        private String method;

        private String desc;

        private String param;

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }
    }
}
